const DEFAULT_MAX_VALUE = 10;
ValueControl = function(gameInfo) {
    this.gameInfo = gameInfo;
    this.range = 6;
    this.stackSize = 8;
    this.arr = [];

    /*
    ** 数组中的内容： 
    ** 0: this.range个
    ** 1: this.range-1个
    ** 以此类推
    ** 生成随机数时的范围 [0,  这个数组长度)，这样可以提升小数字出现的概率，降低大数字出现的概率，降低难度
    */
    this.randomOffsetArr = [];
    for (var i=0; i<this.range; i++) {
        for(j=0; j<(this.range-i)*2; j++) {
            this.randomOffsetArr.push(i);
        }
    }

    this.getNewRandomValue = function(maxValue = null) {
        if (this.arr.length == 0) {
            this.fillRandomArr();
        }
        var v = this.arr.shift();
        if (maxValue != null && v > maxValue) {
            return this.getNewRandomValue(maxValue);
        }
        return v;
    }

    this.getRange = function() {
        if (this.gameInfo.nowMaxValue <= 9) {
            return new IntRange(1, 5);
        } else {
            var max = this.gameInfo.nowMaxValue - 4;
            var min = max - this.range + 1;
            min = Math.max(min, 1);             // 保证 min >= 1
            return new IntRange(min, max);
        }
    }

    this.fillRandomArr = function(cleanStack = false) {
        if (cleanStack) {
            this.arr = [];
        }

        var range = this.getRange();
        while (this.arr.length < this.stackSize) {
            var ramdomIndx = Math.floor(Math.random()*this.randomOffsetArr.length)   // [0, this.randomOffsetArr.length)
            var value = this.randomOffsetArr[ramdomIndx] + range.min;
            if (value > range.max) {
                // 无效生成，继续
                continue;
            }

            this.arr.push(value);
        }
    }

    this.reset = function() {
        this.arr = [];
    }
}